package cn.dingkai.web;

import cn.dingkai.pojo.Province;
import cn.dingkai.service.CityService;
import cn.dingkai.service.impl.CityServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Auther:dingkai
 * @date:2021/12/16 0016 上午 11:37
 * @Desc:
 */
@WebServlet(urlPatterns = "/getCityServlet")
public class getCityServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应数据格式
        response.setContentType("application/json;charset=utf-8");

        //获取页面上的提交过来的parentid=0
        String pid = request.getParameter("pid");//0

        int i = Integer.parseInt(pid);

        //调用业务层
        CityService service = new CityServiceImpl();

        //调方法转pid
        List<Province> list = service.queryById(i);

        //创建json对象
        ObjectMapper mapper = new ObjectMapper();

        //响应给前端
        mapper.writeValue(response.getOutputStream(),list);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request,response);
    }
}
